package com.fayou.aop.aspectaopdemo.controller;

import cn.hutool.core.util.IdUtil;
import com.fayou.cloud.entites.CommonResult;
import com.fayou.cloud.entites.Payment;
import com.fayou.cloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Payment)表控制层
 *
 * @author makejava
 * @since 2020-05-08 21:33:57
 */
@RestController
@RequestMapping("/payment")
@RefreshScope
public class PaymentController {
    @Value("${server.port}")
    private String port;

    @Value(value = "${timeOut}")
    private long timeOut;
    /**
     * 服务对象
     */
    @Resource
    private PaymentService paymentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public CommonResult<Payment> selectOne(@RequestParam Integer id) {
        Payment payment = this.paymentService.queryById(id);
        CommonResult<Payment> commonResult = new CommonResult<>(200, "port:" + port, payment);
        return commonResult;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getById")
    public CommonResult<Payment> getById(@RequestParam Integer id) {
        Payment payment = this.paymentService.getById(String.valueOf(id));
        CommonResult<Payment> commonResult = new CommonResult<>(200, "port:" + port, payment);
        return commonResult;
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/deleteById")
    public CommonResult<Boolean> deleteById(@RequestParam Integer id) {
        Boolean b = this.paymentService.deleteById(id);
        CommonResult<Boolean> commonResult = new CommonResult<Boolean>(200, "port:" + port, b);
        return commonResult;
    }



    @GetMapping("/timeout")
    public String paymentFeignTimeout() {
        try {
           Thread.sleep(timeOut);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "time out "+timeOut+",port:"+port;
    }

    @PostMapping("/addpayment")
    public CommonResult<Payment> insert(@RequestBody Payment payment) {
        Payment p = this.paymentService.insert(payment);
        CommonResult<Payment> commonResult = new CommonResult<>(200, "port:" + port, p);
        return commonResult;
    }

    @GetMapping("/feign/timeout1")
    public String paymentFeignTimeout1() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "timeout344443";
    }


    @GetMapping("/circuitBreaker/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        if (id < 0) {
            throw new RuntimeException("******id 不能负数");
        }
        String serialNumber = IdUtil.simpleUUID();

        return Thread.currentThread().getName() + "\t" + "调用成功，流水号: " + serialNumber;
    }
}